// pages/search/search.js
const { searchDefault, searchHelper, getGoodList} = require('../../api/index.js')
Page({
  data: {
    searchObj: {},
    inptVal: '',
    searchHelperArr: [],
    goodObj: {
      goodsList: []
    },
    goodParams: {
      page: 1,
      size: 10,
      sort: 'id',
      order: 'desc',
      categoryId: 0
    },
    one: true,
    two: false,
    three: false,
    focus: true,
    categoryStatus: false
  },
  async searchDefault() { //搜索页默认进入请求接口
    const res = await searchDefault()
    this.setData({
      searchObj: res
    })
  },
  async searchHelper() { //模糊查询
    const res = await searchHelper(this.data.inptVal)
    this.setData({
      searchHelperArr: res
    })
  },
  setInptVal(e) { //获取input内容,同data做关联
    let {value} = e.detail
    this.setData({
      inptVal: value
    })
    this.searchHelper()
    if(value === "") {
      this.setData({
        one: true,
        two: false,
        three: false
      })
    }else {
      this.setData({
        one: false,
        two: false,
        three: true
      })
    }
  },
  async getGoodList() { //获取商品列表
    this.setData({
      goodObj: {
        goodsList: []
      }
    })
    const { page, size, sort, order, categoryId } = this.data.goodParams
    const res = await getGoodList({
      keyword: this.data.inptVal,
      page,
      size,
      sort,
      order,
      categoryId
    })
    this.setData({
      goodObj: res,
      one: false,
      two: true,
      three: false
    })
  },
  handleFouces() { //聚焦
    if(this.data.inptVal === "") {
      this.setData({
        one: true,
        two: false,
        three: false
      })
    }else {
      this.searchHelper().then(res => {
        this.setData({
          one: false,
          two: false,
          three: true
        })
      })
    }
  },
  _getGoodList(e) { //历史记录点击查询
    let name = e.currentTarget.dataset.name;
    this.setData({
      inptVal: name
    })
    this.getGoodList() 
  },
  clearInput() { //删除input内容 
    this.setData({
      inptVal: '',
      focus: true
    })
  },
  getGoodListByDefault() { //综合排序
    this.setData({
      'goodParams.sort': 'default'
    })
    this.getGoodList()
  },
  getGoodListByPrice() { //按价格排序
    if (this.data.goodParams.sort != 'price') { //从低到高
      this.setData({
        'goodParams.sort': 'price',
        'goodParams.order': 'asc'
      })
    } else {
      if (this.data.goodParams.order == 'asc') { //从高到低
        this.setData({
          'goodParams.order': 'desc'
        })
      } else {
        this.setData({
          'goodParams.order': 'asc'
        })
      }
    }
    this.getGoodList()
  },
  exchangeCategory() { //切换分类选项，显示隐藏
    this.setData({
      categoryStatus: !this.data.categoryStatus
    })
  },
  getGoodListByCate(e) { //通过分类排序
    let id = e.currentTarget.id;
    this.setData({
      'goodParams.categoryId': id
    })
    this.getGoodList()
  },
  getListMore() { //到达底部加载更多
    const { page, size, sort, order, categoryId } = this.data.goodParams;
    const { totalPages } = this.data.goodObj;
    if (page < totalPages) {
      getGoodList({ keyword: this.data.inptVal, page: page + 1, size, sort, order, categoryId }).then(res => {
        // let str = `goodObj.goodsList[${this.data.goodObj.goodsList.length}]`
        const arr = this.data.goodObj.goodsList.concat(res.goodsList)
        this.setData({
          'goodObj.count': res.count,
          'goodObj.currentPage': res.currentPage,
          'goodObj.pageSize': res.pageSize,
          'goodObj.totalPages': res.totalPages,
          'goodObj.goodsList': arr,
          'goodParams.page': res.currentPage
        })
      })
    }
  },
  onLoad(options) {
    this.searchDefault()
  }
})